<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="html"/>

<xsl:variable name="object_list" select="document('object_list.xml')"/>

<xsl:template match="/">
	<html>
	<head><title>SQL Dependency Documentation</title></head>
	<body>
	
	OBJECT SUMMARY
	<br/>
	<xsl:apply-templates select="/object_header/object_summary"/>
	
	<xsl:if test="/object_header/parameter_dependency">
		<br/>
		<br/>OBJECT PARAMETERS
	</xsl:if>
	<xsl:apply-templates select="/object_header/parameter_dependency">
		<xsl:sort data-type="number" select="parameter_order"/>
	</xsl:apply-templates>
	
	<xsl:if test="/object_header/input_dependency">
		<br/>
		<br/>INPUT DEPENDENCIES
		<br/>
	</xsl:if>
	<xsl:apply-templates select="/object_header/input_dependency"/>
	
	<xsl:if test="/object_header/temporary_dependency">
		<br/>
		<br/>TEMPORARY DEPENDENCIES
		<br/>
	</xsl:if>
	<xsl:apply-templates select="/object_header/temporary_dependency"/>
	
	<xsl:if test="/object_header/output_dependency">
		<br/>
		<br/>OUTPUT DEPENDENCIES
		<br/>
	</xsl:if>
	<xsl:apply-templates select="/object_header/output_dependency"/>
	
	<xsl:if test="/object_header/referencing_dependency">
		<br/>
		<br/>REFERENCING DEPENDENCIES
		<br/>
	</xsl:if>
	<xsl:apply-templates select="/object_header/referencing_dependency"/>
	
	<xsl:if test="/object_header/object_comment">
		<br/>
		<br/>OBJECT COMMENTS
		<br/>
	</xsl:if>
	<xsl:apply-templates select="/object_header/object_comment">
		<xsl:sort data-type="number" select="comment_sequence"/>
	</xsl:apply-templates>
	
	</body>
	</html>
</xsl:template>

<xsl:template match="object_summary">
	<br/><xsl:value-of select="./object_type"/>:&#32;
	<xsl:variable name="object_name" select="./dpnd_object_name"/>
	<xsl:choose>
		<xsl:when test="$object_list/object_list/object[@name = $object_name]/object_content = true()" >
			<xsl:variable name="object_path" select="$object_list/object_list/object[@name = $object_name]/object_path"/>
			<a>
				<xsl:attribute name="href">
					..<xsl:value-of select="$object_path"/>/<xsl:value-of select="./dpnd_object_name"/>.txt
				</xsl:attribute>
				<xsl:value-of select="./dpnd_object_name"/>
			</a>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="./dpnd_object_name"/>
		</xsl:otherwise>
	</xsl:choose>
	
	<br/>Returns:&#32;
	<xsl:value-of select="./return_data_type"/>
	
	<br/>Purpose:&#32;
	<xsl:value-of select="./purpose"/>
</xsl:template>

<xsl:template match="parameter_dependency">
	<br/>
	<br/>Parameter<xsl:value-of select="./parameter_order"/>:&#32;
	<xsl:value-of select="./parameter_name"/>
	<br/>Data Type: <xsl:value-of select="./data_type"/>
	<br/>Default Value: <xsl:value-of select="./default_value"/>
	<br/>Parameter Type: <xsl:value-of select="./return_type"/>
	<br/>Parameter Description: <xsl:value-of select="./parameter_desc"/>
</xsl:template>

<xsl:template match="input_dependency">
	<br/>&#09;<xsl:value-of select="./object_type"/>:&#32;
	<xsl:variable name="object_name" select="./dpnd_object_name"/>
	<xsl:choose>
		<xsl:when test="$object_list/object_list/object/@name = $object_name" >
		<xsl:variable name="object_path" select="$object_list/object_list/object[@name = $object_name]/object_path"/>
			<a>
				<xsl:attribute name="href">
					..<xsl:value-of select="$object_path"/>/<xsl:value-of select="./dpnd_object_name"/>.xml
				</xsl:attribute>
				<xsl:value-of select="./dpnd_object_name"/>
			</a>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="./dpnd_object_name"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="temporary_dependency">
	<br/>&#09;<xsl:value-of select="./object_type"/>:&#32;
	<xsl:value-of select="./dpnd_object_name"/>
</xsl:template>

<xsl:template match="output_dependency">
	<br/>&#09;<xsl:value-of select="./change_type"/>:&#32;
	<xsl:value-of select="./object_type"/>:&#32;
	<xsl:value-of select="./dpnd_object_name"/>
</xsl:template>

<xsl:template match="referencing_dependency">
	<br/>&#09;<xsl:value-of select="./object_type"/>:&#32;
	<xsl:variable name="object_name" select="./dpnd_object_name"/>
	<xsl:choose>
		<xsl:when test="$object_list/object_list/object/@name = $object_name" >
			<xsl:variable name="object_path" select="$object_list/object_list/object[@name = $object_name]/object_path"/>
			<a>
				<xsl:attribute name="href">
					..<xsl:value-of select="$object_path"/>/<xsl:value-of select="./dpnd_object_name"/>.xml
				</xsl:attribute>
				<xsl:value-of select="./dpnd_object_name"/>
			</a>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="./dpnd_object_name"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="object_comment">
	<br/><xsl:value-of select="./comment_sequence"/>.&#32;
	<xsl:value-of select="./code_comment"/>
</xsl:template>

</xsl:stylesheet>
